//
//  AY_SearchKeyCell.m
//  AiYue
//
//  Created by mac on 2023/11/9.
//

#import "AY_SearchKeyCell.h"
#import "AY_SearchKeyView.h"

@interface AY_SearchKeyCell ()<SXTagsViewDelegate>

@property (nonatomic, strong) UILabel *info;
@property (nonatomic, strong) UIButton *deleteBtn;
@property (nonatomic, strong) AY_SearchKeyView *keyView;

@end

@implementation AY_SearchKeyCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        self.contentView.backgroundColor = COLOR(@"F7F5F6");
        self.info = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#010101") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(14)] andLines:1 andRadius:0 andAddSubView:self.contentView];
        [self.info mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(YNUM(16));
            make.top.equalTo(self.contentView).offset(YNUM(10));
        }];
        
        self.deleteBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:nil andFont:nil andTitleColor:nil andBg:nil andImage:nil andBgImage:YImage(@"AY_delete") andTarget:self andAction:@selector(deleteData) andRadius:0 andAddSubView:self.contentView];
        [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).offset(-YNUM(16));
            make.centerY.equalTo(self.info);
            make.width.height.mas_equalTo(YNUM(20));
        }];
    }
    return self;
}

- (void)deleteData {
    if ([self.delegate respondsToSelector:@selector(deleteData)]) {
        [self.delegate deleteData];
    }
}

- (void)sendInfo:(NSString *)info andDataSource:(NSMutableArray *)keys {
    self.info.text = info;
    if ([info isEqual:@"搜索历史"]) {
        self.deleteBtn.hidden = NO;
        [self.info mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(YNUM(20));
        }];
    }else {
        self.deleteBtn.hidden = YES;
        [self.info mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(YNUM(10));
        }];
    }
    
    [self.keyView removeFromSuperview];
    float height = [AY_SearchKeyView getTagsViewHeight:keys dic:@{@"type":@"1"}];
    self.keyView = [[AY_SearchKeyView alloc] init];
    self.keyView.propertyDic = @{@"type":@"1"};
    self.keyView.masksToBounds = YES;
    self.keyView.scrollEnabled = NO;
    self.keyView.tagSpace = YNUM(30);
    self.keyView.cornerRadius = YNUM(16);
    self.keyView.borderWidth = 0;
    self.keyView.titleNormalColor = COLOR(@"#010101");
    [self.keyView setTagAry:keys delegate:self];
    [self.contentView addSubview:self.keyView];
    [self.keyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(YNUM(5));
        make.right.equalTo(self.contentView).offset(-YNUM(5));
        make.top.equalTo(self.info.mas_bottom).offset(YNUM(5));
        make.height.mas_equalTo(height);
    }];
}

#pragma mark SXTagsViewDelegate

/**
 *  tagsView代理方法
 *
 *  @param tagsView tagsView
 *  @param sender   tag:sender.titleLabel.text index:sender.tag
 */
- (void)tagsViewButtonAction:(SXTagsView *)tagsView button:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(didSelectWithKey:)]) {
        [self.delegate didSelectWithKey:sender.titleLabel.text];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
